package com.aaa.controller.role;

import com.aaa.entity.ResponseDto;
import com.aaa.entity.Role;
import com.aaa.service.RoleService;
import com.aaa.service.impl.RoleServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateRoleServlet")
public class UpdateRoleServlet extends HttpServlet {
    private RoleService roleService;
    private Role role;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String roleName = req.getParameter("roleName");
        String description = req.getParameter("description");
        String status = req.getParameter("status");
        role = new Role();
        role.setId(Integer.parseInt(roleId));
        role.setRoleName(roleName);
        role.setDescription(description);
        role.setStatus(Integer.parseInt(status));
        int num = roleService.updateRoleById(role);

        ResponseDto responseDto = new ResponseDto();
        if (num > 0 ){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setMessage("添加成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("添加失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    public void init() throws ServletException {
        roleService = new RoleServiceImpl();
    }
}
